#include "stdafx.h"
#include "FontClass.h"


CFontClass::CFontClass(void) : m_pFont(nullptr), m_pTexture(nullptr)
{
}

CFontClass::~CFontClass(void)
{
}

bool CFontClass::Initialize(ID3D11Device* pDevice, char* cFontFileName, WCHAR* pwszFileName)
{
	bool bResult = false;

	bResult = LoadFontData(cFontFileName);
	if(bResult == false)
	{
		DEBUG_OUTPUT("Failed to load font data");
		return false;
	}

	bResult = LoadTexture(pDevice, pwszFileName);
	if(bResult == false)
	{
		DEBUG_OUTPUT("Failed to load font texture");
		return false;
	}

	return true;
}

void CFontClass::ShutDown()
{
	ReleaseTexture();
	ReleaseFontData();
}

ID3D11ShaderResourceView* CFontClass::GetTexture()
{
	return m_pTexture->GetTexture();
}

void CFontClass::BuildVertexArray(void* pVertices, char* cSentence, float fDrawX, float fDrawY)
{
	VertexType* pVertexPtr = (VertexType*)pVertices;
	int nNumLetters = (int)strlen(cSentence);
	int nIndex = 0;
	int nLetter = 0;

	for(int i = 0; i < nNumLetters; ++i)
	{
		nLetter = ((int)cSentence[i]) - 32;

		if(nLetter == 0)
		{
			fDrawX += 3.0f;
		}
		else
		{
			pVertexPtr[nIndex].d3dPosition = D3DXVECTOR3(fDrawX, fDrawY, 0.0f);  // Top left.
			pVertexPtr[nIndex].d3dTexture = D3DXVECTOR2(m_pFont[nLetter].fLeft, 0.0f);
			nIndex++;

			pVertexPtr[nIndex].d3dPosition = D3DXVECTOR3((fDrawX + m_pFont[nLetter].nSize), (fDrawY - 16), 0.0f);  // Bottom right.
			pVertexPtr[nIndex].d3dTexture = D3DXVECTOR2(m_pFont[nLetter].fRight, 1.0f);
			nIndex++;

			pVertexPtr[nIndex].d3dPosition = D3DXVECTOR3(fDrawX, (fDrawY - 16), 0.0f);  // Bottom left.
			pVertexPtr[nIndex].d3dTexture = D3DXVECTOR2(m_pFont[nLetter].fLeft, 1.0f);
			nIndex++;

			pVertexPtr[nIndex].d3dPosition = D3DXVECTOR3(fDrawX, fDrawY, 0.0f);  // Top left.
			pVertexPtr[nIndex].d3dTexture = D3DXVECTOR2(m_pFont[nLetter].fLeft, 0.0f);
			nIndex++;

			pVertexPtr[nIndex].d3dPosition = D3DXVECTOR3(fDrawX + m_pFont[nLetter].nSize, fDrawY, 0.0f);  // Top right.
			pVertexPtr[nIndex].d3dTexture = D3DXVECTOR2(m_pFont[nLetter].fRight, 0.0f);
			nIndex++;

			pVertexPtr[nIndex].d3dPosition = D3DXVECTOR3((fDrawX + m_pFont[nLetter].nSize), (fDrawY - 16), 0.0f);  // Bottom right.
			pVertexPtr[nIndex].d3dTexture = D3DXVECTOR2(m_pFont[nLetter].fRight, 1.0f);
			nIndex++;

			fDrawX = fDrawX + m_pFont[nLetter].nSize + 1.0f;
		}
	}
}

bool CFontClass::LoadFontData(char* cFontFileName)
{
	ifstream fin;
	char cTemp;

	m_pFont = new FontType[95];
	if(m_pFont == nullptr)
	{
		DEBUG_OUTPUT("Failed to create new font pointer");
		return false;
	}

	fin.open(cFontFileName);
	if(fin.fail())
	{
		DEBUG_OUTPUT("Failed to open font file");
		return false;
	}

	for(int i = 0; i <= 94; ++i)
	{
		fin.get(cTemp);

		while(cTemp != ' ')
		{
			fin.get(cTemp);
		}
		fin.get(cTemp);
		while(cTemp != ' ')
		{
			fin.get(cTemp);
		}

		fin >> m_pFont[i].fLeft;
		fin >> m_pFont[i].fRight;
		fin >> m_pFont[i].nSize;
	}

	fin.close();

	return true;
}

void CFontClass::ReleaseFontData()
{
	if(m_pFont != nullptr)
	{
		delete[] m_pFont;
		m_pFont = nullptr;
	}
}

bool CFontClass::LoadTexture(ID3D11Device* pDevice, WCHAR* pwszFileName)
{
	bool bResult = false;

	m_pTexture = new CTextureClass();
	if(m_pTexture == nullptr)
	{
		DEBUG_OUTPUT("Failed to create new texture class pointer");
		return false;
	}

	bResult = m_pTexture->Initialize(pDevice, pwszFileName);
	if(bResult == false)
	{
		DEBUG_OUTPUT("Failed to initialize texture pointer");
		return false;
	}

	return true;
}

void CFontClass::ReleaseTexture()
{
	if(m_pTexture != nullptr)
	{
		m_pTexture->ShutDown();
		delete m_pTexture;
		m_pTexture = nullptr;
	}
}